/*
 * Copyright (c) Huawei Device Co., Ltd. 2024. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef BER_TLV_H
#define BER_TLV_H
#include <string>
#include <map>
#include "loghelper.h"

namespace OHOS {
namespace SE {
using namespace std;
enum {
    BER_RET_SUCCESS = 0,
    BER_RET_FAIL = 1,
    BER_RET_HEADER_ERROR = 2,
    BER_RET_PAYLOAD_ERROR = 3,
    BER_RET_LENGTH_ERROR = 4,
    BER_RET_END,
};

#define RESPONSE_ALL_REF_AR_DO_TAG 0xFF40
#define REF_AR_DO_TAG 0xE2
#define REF_DO_TAG 0xE1
#define AR_DO_TAG 0xE3
#define AID_REF_DO_EMPTY_TAG 0xC0 // select all app
#define AID_REF_DO_TAG 0x4F
#define DEVICE_APPID_REF_DO_TAG 0xC1
#define APDU_AR_DO_TAG 0xD0
#define NFC_AR_DO_TAG 0xD1
#define PERM_AR_DO_TAG 0xD8
#define RESPONSE_REFRESH_TAG_DO 0xDF20

#define TAG_LENGTH_TWO_BYTES 2
#define TAG_LENGTH_ONE_BYTE 1
#define BIT_NUM_PER_BYTE 8

#define ONE_BYTE_LEN_MAX_NUM 0x80
#define TWO_BYTES_LEN_MAX_NUM 0x100
#define THREE_BYTES_LEN_MAX_NUM 0x10000
#define TWO_BYTES_LEN_FLAG 0x81
#define THREE_BYTES_LEN_FLAG 0x82
#define ONE_BYTE_MASK 0xFF
/*
 * Software that is part of the Secure Element access API, it obtains access
 * rules from the Secure Element and applies those rules to restrict device
 * application access to the various Secure Element applications.
 */
class BerTlv {
public:
    BerTlv(std::vector<uint8_t> rawData, uint32_t doTag);
    BerTlv(std::vector<uint8_t> payload, bool isPayload, uint32_t tag);
    ~BerTlv();
    bool GetDoTag(uint32_t &tag);
    bool GetDoLength(uint32_t &dataStartIndex, uint32_t &length);
    bool GetDoPayload(vector<uint8_t> &payload);
    void ParseLength(const std::vector<uint8_t> &data, uint32_t &length, uint32_t &dataStartIndex);
    void GetRawData(std::vector<uint8_t> &rawData);

private:
    std::vector<uint8_t> rawData_;
    uint32_t doTag_;
};
}  // namespace SE
}  // namespace OHOS
#endif // BER_TLV_H
